<?php
	
require_once("DBMySQL.class.php");

function updateCheckin($quest_id) {
	
	$ret = array('result' => 'fail');
	
	try {
		$oDB = new DBMySQL();
		
		//$result = $oDB->query("SELECT last_chk_date from quests where id = '$quest_id'");
		// update quests table (last_chk_date update) 
		$today = date("Y-m-d");
		$result = $oDB->query("UPDATE quests SET last_chk_date = '$today' where id='$quest_id'");
		
		if( $result == false) {
			throw new Exception('updateCheckin: UPDATE query is failed.');
		}
		
		// and then update checkin table
		$resultChk = $oDB->query("INSERT INTO checkins SET quest_id='$quest_id', quest_chk_date='$today'");
		
		if( $resultChk == false) {
			throw new Exception('updateCheckin: INSERT query is failed.');
		}
		
		$ret = array('result' => 'success');
		
		$oDB->close();
		$oDB = null;
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	return json_encode($ret);
}

function cancelCheckin($quest_id) {
	
	// checkins 말고 quests 의 last_chk_date 도 변경해주어야 함. 
	$ret = array('result' => 'fail');
	
	try {
		$oDB = new DBMySQL();
		
		$today = date("Y-m-d");
		
		$result = $oDB->query("DELETE from checkins where quest_id='$quest_id' and quest_chk_date='$today'");
		// need more work !!!
		if( $result == false) {
			throw new Exception('cancelCheckin: DELETE query is failed.');
		}
				
		$resultLstChk = $oDB->query("UPDATE quests SET last_chk_date = 
							(SELECT MAX( quest_chk_date ) FROM checkins WHERE checkins.quest_id='$quest_id')
							WHERE quests.id = '$quest_id' ");	
				
		//$resultLstChk = $oDB->query("SELECT quest_chk_date FROM checkins where quest_chk_date IN (SELECT MAX( quest_chk_date ) FROM checkins WHERE quest_id='$quest_id') ORDER BY quest_chk_date DESC");
				
		if ($resultLstChk == true) {
			// $row = $resultLstChk->fetch_assoc();
			// $new_chk_date = $row['quest_chk_date'];
					
			
			$ret = array('result' => 'success');	
		}		
		
		$oDB->close();
		$oDB = null;
	} catch(Exception $e) {
		error_log('Caught Exception : ' . $e->getMessage() . '\n');
	}
	return json_encode($ret);
}

function getCheckins($quest_id) {
    $checkins = getCheckinsPHP($quest_id);
    
    return json_encode( $checkins );
    
}

function getCheckinsPHP($quest_id) {
    $checkins = array();
    try {
        $oDB = new DBMySQL();
        
        $result = $oDB->query("SELECT * from checkins where quest_id = '$quest_id'");
        
        if( $result == false) {
            throw new Exception('getCheckins: SELECT query is failed.');
        }
        for( $i = 0; $row = $result->fetch_assoc(); $i++ ) {
            $checkin = array( 'quest_chk_date' => $row['quest_chk_date'],
                           'quest_chk_type' => $row['quest_chk_type'],
                           'quest_value' => $row['quest_value'] );
            array_push($checkins, $checkin);
        }
        
        $oDB->close();
		$oDB = null;
    } catch(Exception $e) {
        error_log('Caught Exception : ' . $e->getMessage() . '\n');
    }
    
    return $checkins ;
    
}
	
?>